<?php


namespace app\apibook\service;


use think\facade\Log;
use think\facade\Request;
use think\helper\Arr;

class BaseService
{
    protected $model = null;
    protected $modelNamespace = null;

    public function __construct($class)
    {
        $classNamespace = explode('\\', $class);
        $serviceName = array_pop($classNamespace);
        $modelName = str_replace('Service', 'Model', $serviceName);
        $classNamespace = implode('\\', $classNamespace);
        $classNamespace = str_replace('service', 'model', $classNamespace);
        $this->modelNamespace = $classNamespace . '\\' . $modelName;
        if (!$this->model && class_exists($this->modelNamespace)) {
            $this->setModel($this->modelNamespace);
        }
    }

    public function setModel($model = '')
    {

        $model = empty($model) ? $this->modelNamespace : $model;
        $this->model = new $model();
    }

    /**
     * @param $id
     * @return array
     * Created by chenyawei on 2024/8/7
     * Description:根据id 查询用户
     */
    public function findById($id, $fields = ['*'])
    {
        $items = $this->model->field($fields)->find($id);
        return $items ? $items->toArray() : [];
    }


    public function checkExist($where)
    {
        return $this->model->where($where)->count();
    }


    public function findByCondition($where, $columns = ['*'], $sortrow = "id", $sort = 'desc',$groupBy='')
    {

        $items = $this->model->where($where)->field($columns)->order($sortrow, $sort);
        if($groupBy){
            $items = $items->group($groupBy);
        }
        $items = $items->select();
        //$items = $this->model->where($where)->field($columns)->order($sortrow, $sort)->select();
        return $items ? $items->toArray() : [];
    }
    public function findOneByCondition($where, $columns = ['*'], $sortrow = "id", $sort = 'desc')
    {
        $item = $this->model->where($where)->field($columns)->order($sortrow, $sort)->find();
        return $item ? $item->toArray() : [];
    }

    public function updateByCondition($where, $data)
    {
        return  $this->model->where($where)->save($data);
    }

    public function updateById($id, $data)
    {
        return  $this->model->where(['id' => $id])->save($data);
    }

    public function softDelById($id)
    {
        return  $this->model->where(['id' => $id])->save(['del' => 1]);
    }

    public  function searchOneByCondition($where = array(), $fields = ['*'])
    {
        $items = $this->model->where($where)->field($fields)->find();
        return $items ? $items->toArray() : [];
    }

    public function destory($delId)
    {
        return ($this->model)::destroy($delId);
    }
    public function deleteByCondition($where)
    {
        return  $this->model->where($where)->delete();
    }

    public function insertGetId($data)
    {
        return ($this->model)->insertGetId($data);
    }
    public function create($data)
    {
        return ($this->model)->create($data);
    }

    public function createOnly($data)
    {
        $data = Arr::only($data, $this->model->createColumn());
        return $this->model->create($data);
    }

    public function editOnly($where, $data)
    {
        $data = Arr::only($data, $this->model->editColumn());
        return $this->model->where($where)->save($data);
    }

    public function batchSave($data)
    {
        return  $this->model->insertAll($data);
    }

    public function getColumnByCondition($where, $column)
    {
        return $this->model->where($where)->column($column);
    }

    public function getValueByCondition($where, $value)
    {
        return $this->model->where($where)->value($value);
    }

    public function findByConditionNum($where, $columns = ['*'], $sortrow = "id", $sort = 'desc', $limit = 20)
    {
        $items = $this->model->where($where)->field($columns)->order($sortrow, $sort)->limit($limit)->select();
        return $items ? $items->toArray() : [];
    }

    public function getMaxFeldVal($where, $column)
    {
        return $this->model->where($where)->max($column);
    }

    public function getMinFeldVal($where, $column)
    {
        return $this->model->where($where)->min($column);
    }
}
